'''
    股票问题类
'''
import sys


class Stock:
    '''
    计算可能的最大利益
    '''

    def getMaxProfit(self, prices: []) -> int:

        min_price = sys.maxsize
        max_profit = 0

        for p in prices:

            if min_price > p:
                min_price = p
            elif (p - min_price) > max_profit:
                max_profit = p - min_price

        return max_profit


class BaseNum:

    def __init__(self):
        self.tab = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']

    def getMajorityNum(self, nums: []) -> int:

        temp = {}

        for num in nums:
            v = temp.get(num, 0) + 1
            temp[num] = v

        max_val = 0;
        majority_num = None

        for k, v in temp.items():
            if v > max_val:
                max_val = v
                majority_num = k

        return majority_num

    def letterCombinations(self, digits: str) -> []:

        st = [self.tab[int(x) - 2] for x in digits]
        res = []

        return self.recursiveCombinations(st, res)

    def recursiveCombinations(self, str: [], res: []):

        if not str:
            return res
        if not res:
            res = ['']

        cs = str.pop()
        res2 = []

        for c in cs:
            res2 += [c + x for x in res]

        return self.recursiveCombinations(str, res2)

    '''
        判断一个数是不是快乐数
    '''

    def isHappyNum(self, num: int) -> bool:
        temp = num
        container = [temp]

        while True:

            temp = self.getNextNum(temp)

            if temp == 1:
                return True
            elif temp in container:
                return False

            container.append(temp)

    '''
        计算下一个数 判断是不是快乐数
    '''

    def getNextNum(self, num: int) -> int:

        next_num = 0
        while num > 0:
            next_num += (num % 10) ** 2
            num = int(num / 10)

        return next_num


class String:

    def reverseString(self, str: [str]) -> None:
        head = 0
        tail = len(str) - 1

        while head < tail:
            temp = str[head]
            str[head] = str[tail]
            str[tail] = temp
            tail -= 1
            head += 1

        print('reverse string', str)

    def firstUniqChar(self, s: str) -> int:

        if not s:
            return -1

        l = len(s)
        if l < 2:
            return 0
        is_same = False
        for i in range(l):
            for j in range(l):
                if i == j:
                    continue
                if s[i] == s[j]:
                    is_same = True
                    break

            if not is_same:
                return i
            is_same = False

        return -1
